package ystp.service;

import jxl.SheetSettings;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import ystp.dao.ZhuankeyaopinjieguoMapper;
import ystp.model.TongkeyaopinjieguoExample;
import ystp.model.Ysuser;
import ystp.model.Zhuankeyaopinjieguo;
import ystp.model.ZhuankeyaopinjieguoExample;
import ystp.support.StringHandle;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Service
public class ZhuankeyaopinjieguoService {

    @Resource
    ZhuankeyaopinjieguoMapper ddMapper;

    @Resource
    ZhuankeyaopinService ddZhuankeyaopinService;

    @Resource
    YsuserService ddYsuserService;

    public int countByDetailId(String ppDetailId,String ppResult){
        ZhuankeyaopinjieguoExample mmExample=new ZhuankeyaopinjieguoExample();
        mmExample.createCriteria().andDetailidEqualTo(ppDetailId).andResultEqualTo("1");
        return ddMapper.countByExample(mmExample);
    }

    // 导出某个委员的签字表
    public void daochuqianzibiao(@RequestParam("xiangmuid") String ppxiangmuid,
                                 @RequestParam("userid") String ppuserid,
                                 HttpServletRequest request, HttpServletResponse response){

        response.reset();
        response.setContentType("application/x-download;charset=utf-8");
        List<Map<String,Object>> mmList = ddZhuankeyaopinService.findZhuankeBiaojueByXiangmuId(ppxiangmuid,ppuserid,1,9999);
        Ysuser mmuser=ddYsuserService.findOne(ppuserid);
        String mmusername=mmuser.getUsername();
        OutputStream os=null;
        WritableWorkbook wbook=null;
        try
        {
            os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句
            //response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
            String wenjiantitle = "专科药品目录"+mmusername+"委员结果表"+ StringHandle.tiemString(); //文件名
            String tmptitle = "东部战区总医院专科药品目录投票表（"+mmusername+"）"; // 标题
            response.setHeader("Content-disposition", "attachment; filename="+new String(wenjiantitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os); // 建立excel文件
            WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
            SheetSettings settings=wsheet.getSettings();
          /*  settings.setPassword("0000");*/
            settings.setProtected(true);

            // 设置excel标题
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式

            WritableCellFormat wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); // 添加边框
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);

            WritableCellFormat wcfFC3 = new WritableCellFormat(wfont);
            wcfFC3.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC3.setAlignment(Alignment.CENTRE);
            //wcfFC.setBackground(Colour.AQUA);
            Label label=new Label(0, 0, tmptitle, wcfFC3);

            WritableCellFormat cellFormat=new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat.setWrap(true);
            //cellFormat.setBackground(Colour.AQUA);
            cellFormat.setFont(wfont);
            label.setCellFormat(cellFormat);
            wsheet.addCell(label);
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC3));
            /*wsheet.addCell(new Label(7, 0, tmptitle, wcfFC));*/
            wsheet.setRowView(0,600); //第一行高度
            wsheet.setRowView(1,600); //第二行高度
            wsheet.mergeCells(0, 0, 23, 0);  //合并单元格(第一列的第一行和第十三列的第一行合并)
            wsheet.setColumnView(0,5); //序号宽度
            wsheet.setColumnView(1,10); //药品名称宽度
            wsheet.setColumnView(2,10); //规格宽度
            wsheet.setColumnView(3,4); //单位宽度
            wsheet.setColumnView(4,5); //医保宽度
            wsheet.setColumnView(5,4); //基药宽度
            wsheet.setColumnView(6,4); //1采集宽度
            wsheet.setColumnView(7,4); //2宽度
            wsheet.setColumnView(8,4); //3宽度
            wsheet.setColumnView(9,4); //4宽度
            wsheet.setColumnView(10,4); //5宽度
            wsheet.setColumnView(11,4); //6宽度
            wsheet.setColumnView(12,4); //1宽度
            wsheet.setColumnView(13,4); //2宽度
            wsheet.setColumnView(14,4); //3宽度
            wsheet.setColumnView(15,4); //1宽度
            wsheet.setColumnView(16,4); //2宽度
            wsheet.setColumnView(17,6); //1宽度
            wsheet.setColumnView(18,6); //2宽度
            wsheet.setColumnView(19,8); //宽度
            wsheet.setColumnView(20,6); //宽度
            wsheet.setColumnView(21,6); //宽度
            wsheet.setColumnView(22,8); //宽度
            wsheet.setColumnView(23,8); //宽度




            // 开始生成主体内容
            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 6,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);
            // 不需要边框和居中的样式
            WritableFont wfont2 = new WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式
            WritableCellFormat wcfFC2 = new WritableCellFormat(wfont2);
            wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 垂直居中
            wcfFC2 = new WritableCellFormat(wfont2);
            wcfFC2.setWrap(true);
            //  在  1 列  2 行 写入内容
            wsheet.addCell(new Label(0, 1, "投票人(签字):投票内容已核对无误。"+mmusername,wcfFC2));
            // 把  0 列 2 行 到 9 列 2 行合并
            wsheet.mergeCells(0, 1, 18, 1);


            wsheet.addCell(new Label(19, 1, "投票日期："+StringHandle.shorttiemString(),wcfFC2));
            wsheet.mergeCells(19, 1, 23, 1);

            // 第三行 内容
            wsheet.addCell(new Label(0, 2, "序号",wcfFC));
            wsheet.mergeCells(0, 2, 0, 3);
            wsheet.addCell(new Label(1, 2, "药品名称",wcfFC));
            wsheet.mergeCells(1, 2, 1, 3);
            wsheet.addCell(new Label(2, 2, "规格",wcfFC));
            wsheet.mergeCells(2, 2, 2, 3);
            wsheet.addCell(new Label(3, 2, "单位",wcfFC));
            wsheet.mergeCells(3, 2, 3, 3);
        /*    wsheet.addCell(new Label(4, 2, "价格（元）",wcfFC));
            wsheet.mergeCells(4, 2, 4, 3);*/
            wsheet.addCell(new Label(4, 2, "医保类别",wcfFC));
            wsheet.mergeCells(4, 2, 4, 3);
            wsheet.addCell(new Label(5, 2, "基药",wcfFC));
            wsheet.mergeCells(5, 2, 5, 3);

            wsheet.addCell(new Label(6, 2, "建议保留条件",wcfFC));
            wsheet.mergeCells(6, 2, 11, 2);

            wsheet.addCell(new Label(12, 2, "建议淘汰条件",wcfFC));
            wsheet.mergeCells(12, 2, 14, 2);

            wsheet.addCell(new Label(15, 2, "所在目录",wcfFC));
            wsheet.mergeCells(15, 2, 16, 2);

            wsheet.addCell(new Label(17, 2, "半年无采购记录",wcfFC));
            wsheet.mergeCells(17, 2, 18, 2);

            wsheet.addCell(new Label(19, 2, "专科分组",wcfFC));
            wsheet.mergeCells(19, 2, 19, 3);

            wsheet.addCell(new Label(20, 2, "专科组预选意见",wcfFC));
            wsheet.mergeCells(20, 2, 20, 3);

            wsheet.addCell(new Label(21, 2, "预选会意见（新药、中药）",wcfFC));
            wsheet.mergeCells(21, 2, 21, 3);

            wsheet.addCell(new Label(22, 2, "备注",wcfFC));
            wsheet.mergeCells(22, 2, 22, 3);

            wsheet.addCell(new Label(23, 2, "表决意见",wcfFC));
            wsheet.mergeCells(23, 2, 23, 3);
            // 第四行内容

            wsheet.addCell(new Label(6, 3, "集采药",wcfFC));
            wsheet.addCell(new Label(7, 3, "中药保密配方",wcfFC));
            wsheet.addCell(new Label(8, 3, "国家中药保护",wcfFC));
            wsheet.addCell(new Label(9, 3, "原国谈药",wcfFC));
            wsheet.addCell(new Label(10, 3, "创新药",wcfFC));
            wsheet.addCell(new Label(11, 3, "重点监控药",wcfFC));
            wsheet.addCell(new Label(12, 3, "中药注射剂",wcfFC));
            wsheet.addCell(new Label(13, 3, "严重不良反应",wcfFC));
            wsheet.addCell(new Label(14, 3, "掉网药",wcfFC));
            wsheet.addCell(new Label(15, 3, "院本部",wcfFC));
            wsheet.addCell(new Label(16, 3, "秦淮",wcfFC));
            wsheet.addCell(new Label(17, 3, "院本部",wcfFC));
            wsheet.addCell(new Label(18, 3, "秦淮",wcfFC));

            // 表头结束

            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 6,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            int count=mmList.size();
            int mmKaishihang=4;

            for(int i=0;i<count;i++){
                Map<String,Object> mmMap = mmList.get(i);
                if (mmMap!=null) {
                    String mmxuhao = mmMap.get("xuhao") == null ? "" : mmMap.get("xuhao").toString();
                    String mmyaopinmingcheng = mmMap.get("yaopinmingcheng") == null ? "" : mmMap.get("yaopinmingcheng").toString();
                    String mmguige = mmMap.get("guige") == null ? "" : mmMap.get("guige").toString();
                    String mmdanwei = mmMap.get("danwei") == null ? "" : mmMap.get("danwei").toString();
                    String mmjiage = mmMap.get("jiage") == null ? "" : mmMap.get("jiage").toString();
                    String mmyibaoleibie = mmMap.get("yibaoleibie") == null ? "" : mmMap.get("yibaoleibie").toString();
                    String mmguojiajibenyaowu = mmMap.get("guojiajibenyaowu") == null ? "" : mmMap.get("guojiajibenyaowu").toString();
                    String mmjicaiyao = mmMap.get("jicaiyao") == null ? "" : mmMap.get("jicaiyao").toString();
                    String mmzhongyaobaohu = mmMap.get("zhongyaobaohu") == null ? "" : mmMap.get("zhongyaobaohu").toString();
                    String mmbaomipeifang = mmMap.get("baomipeifang") == null ? "" : mmMap.get("baomipeifang").toString();
                    String mmyuanguotanyao = mmMap.get("yuanguotanyao") == null ? "" : mmMap.get("yuanguotanyao").toString();
                    String mmchuanxinyao = mmMap.get("chuanxinyao") == null ? "" : mmMap.get("chuanxinyao").toString();
                    String mmjiankongyao = mmMap.get("jiankongyao") == null ? "" : mmMap.get("jiankongyao").toString();
                    String mmzhusheji = mmMap.get("zhusheji") == null ? "" : mmMap.get("zhusheji").toString();
                    String mmbuliangfanying = mmMap.get("buliangfanying") == null ? "" : mmMap.get("buliangfanying").toString();
                    String mmdiaowangyao = mmMap.get("diaowangyao") == null ? "" : mmMap.get("diaowangyao").toString();

                    String mmdongzongyao = mmMap.get("dongzongyao") == null ? "" : mmMap.get("dongzongyao").toString();
                    String mmdongzongjilu2 = mmMap.get("dongzongjilu2") == null ? "" : mmMap.get("dongzongjilu2").toString();
                    String mmqinhuaiyao = mmMap.get("qinhuaiyao") == null ? "" : mmMap.get("qinhuaiyao").toString();
                    String mmqinhuaijilu2 = mmMap.get("qinhuaijilu2") == null ? "" : mmMap.get("qinhuaijilu2").toString();
                    String mmbeizhu = mmMap.get("beizhu") == null ? "" : mmMap.get("beizhu").toString();
                    String mmbeizhu2 = mmMap.get("beizhu2") == null ? "" : mmMap.get("beizhu2").toString();
                    String mmzhuankefenzu = mmMap.get("zhuankefenzu") == null ? "" : mmMap.get("zhuankefenzu").toString();
                    String mmyuxuanyijian = mmMap.get("yuxuanyijian") == null ? "" : mmMap.get("yuxuanyijian").toString();


                    String mmresult = mmMap.get("result") == null ? "" : mmMap.get("result").toString();
                    String mmbiaojueren = mmMap.get("username") == null ? "" : mmMap.get("username").toString();
                    String mmyijian = "";
                    if ("1".equals(mmresult)) {
                        mmyijian = "同意";
                    } else if ("0".equals(mmresult)) {
                        mmyijian = "不同意";
                    } else {
                        mmyijian = "不同意";
                    }
                    wsheet.addCell(new Label(0, mmKaishihang, mmxuhao, wcfFC));
                    wsheet.addCell(new Label(1, mmKaishihang, mmyaopinmingcheng, wcfFC));
                    wsheet.addCell(new Label(2, mmKaishihang, mmguige, wcfFC));
                    wsheet.addCell(new Label(3, mmKaishihang, mmdanwei, wcfFC));
                 /*   wsheet.addCell(new Label(4, mmKaishihang, mmjiage, wcfFC));*/
                    wsheet.addCell(new Label(4, mmKaishihang, mmyibaoleibie, wcfFC));
                    wsheet.addCell(new Label(5, mmKaishihang, mmguojiajibenyaowu, wcfFC));
                    wsheet.addCell(new Label(6, mmKaishihang, mmjicaiyao, wcfFC));
                    wsheet.addCell(new Label(7, mmKaishihang, mmbaomipeifang, wcfFC));
                    wsheet.addCell(new Label(8, mmKaishihang, mmzhongyaobaohu, wcfFC));
                    wsheet.addCell(new Label(9, mmKaishihang, mmyuanguotanyao, wcfFC));
                    wsheet.addCell(new Label(10, mmKaishihang, mmchuanxinyao, wcfFC));
                    wsheet.addCell(new Label(11, mmKaishihang, mmjiankongyao, wcfFC));
                    wsheet.addCell(new Label(12, mmKaishihang, mmzhusheji, wcfFC));
                    wsheet.addCell(new Label(13, mmKaishihang, mmbuliangfanying, wcfFC));
                    wsheet.addCell(new Label(14, mmKaishihang, mmdiaowangyao, wcfFC));
                    wsheet.addCell(new Label(15, mmKaishihang, mmdongzongyao, wcfFC));
                    wsheet.addCell(new Label(16, mmKaishihang, mmqinhuaiyao, wcfFC));
                    wsheet.addCell(new Label(17, mmKaishihang, mmdongzongjilu2, wcfFC));
                    wsheet.addCell(new Label(18, mmKaishihang, mmqinhuaijilu2, wcfFC));
                    wsheet.addCell(new Label(19, mmKaishihang, mmzhuankefenzu, wcfFC));
                    wsheet.addCell(new Label(20, mmKaishihang, mmyuxuanyijian, wcfFC));
                    wsheet.addCell(new Label(21, mmKaishihang, mmbeizhu2, wcfFC));
                    wsheet.addCell(new Label(22, mmKaishihang, mmbeizhu, wcfFC));
                    wsheet.addCell(new Label(23, mmKaishihang, mmyijian, wcfFC));
                    mmKaishihang = mmKaishihang+1;

                }
            }
            wbook.write(); // 写入文件
            wbook.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public Integer getZHuanKeResultCount(String mmxiangmuId,String mmuserId,String mmresult){
        Object mmCount = ddMapper.getZhuanKeResultCount(mmxiangmuId,mmuserId,mmresult);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public Integer getGuizeCount(String mmuserId,String mmxiangmuId,String mmguizebianhao){
        int count =ddMapper.getGuizeCount(mmuserId,mmxiangmuId,mmguizebianhao).size();
        return count;
    }

    public Integer getJiCaiAgreedCount(String mmuserId,String mmxiangmuId){
        int count =ddMapper.getJiCaiAgreedCount(mmuserId,mmxiangmuId).size();
        return count;
    }

    public Integer getZhuSheJiAgreedCount(String mmuserId,String mmxiangmuId){
        int count =ddMapper.getZhuSheJiAgreedCount(mmuserId,mmxiangmuId).size();
        return count;
    }

    public Integer getChuangXinAgreedCount(String mmuserId,String mmxiangmuId){
        int count =ddMapper.getChuangXinAgreedCount(mmuserId,mmxiangmuId).size();
        return count;
    }

    public Integer getJunGongAgreedCount(String mmuserId,String mmxiangmuId){
        int count =ddMapper.getJunGongAgreedCount(mmuserId,mmxiangmuId).size();
        return count;
    }

    public List<Zhuankeyaopinjieguo> findByDetailIdAndUserId(String ppdetailid,String ppuserid){
        ZhuankeyaopinjieguoExample mmExample=new ZhuankeyaopinjieguoExample();
        ZhuankeyaopinjieguoExample.Criteria criteria = mmExample.createCriteria();
        criteria.andDetailidEqualTo(ppdetailid).andUseridEqualTo(ppuserid);
        return ddMapper.selectByExample(mmExample);
    }

    public Zhuankeyaopinjieguo findone(String mmid){
        return ddMapper.selectByPrimaryKey(mmid);
    }

    public Zhuankeyaopinjieguo checkexistByUserId(String ppuserId,String ppdetailId){
        ZhuankeyaopinjieguoExample mmExample=new ZhuankeyaopinjieguoExample();
        ZhuankeyaopinjieguoExample.Criteria criteria = mmExample.createCriteria();
        criteria.andUseridEqualTo(ppuserId).andDetailidEqualTo(ppdetailId);
        return ddMapper.selectByExample(mmExample).size()>0? ddMapper.selectByExample(mmExample).get(0): null;
    }

    public boolean modify(Zhuankeyaopinjieguo mmZhuankeyaopinjieguo){
        return ddMapper.updateByPrimaryKeySelective(mmZhuankeyaopinjieguo)==1;
    }

    public boolean add(Zhuankeyaopinjieguo mmZhuankeyaopinjieguo){
        return ddMapper.insert(mmZhuankeyaopinjieguo)==1;
    }

}
